Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### FileListBox The FileListBox control displays a list of all files/folders or selected files/folders in a specified folder, and the user can select from the list. The folder whose files are listed is determined by the control’s Path property. Thus, to list the files in the folder \DATA on drive C:, you would write (assuming that File1 is the Name property of a FileListBox control):
    File1.Path = “c:\data”
Often, the Path property for a FileListBox is obtained from a Directory List Box control, as you’ll see later. The FileListBox control has several other properties you should know. The Pattern property specifies the template for file names to be displayed. The default Pattern is “*.*”, which displays all files. Other Patterns let you limit the display to subsets of files. For example, to display only files with the .DAT extension, you would write:
    File1.Pattern = “*.DAT”
You can use multiple templates separated by semicolons. Thus, the code
    File1.Pattern = “*.DAT;*.TXT;*.BAT”
would display only those files with the .DAT, .TXT, and .BAT extensions. The Filename property returns the name of the file currently selected in the list. If no file is selected, this property returns a zero-length string. If you are using FileListBox to permit the user to select a file, you would retrieve the name of the selected file from the Filename property. You obtain the path of the selected file separately from the control’s Path property. For example, if File1 is the name of a FileListBox control, the following code would open the file selected by the user:
    Dim MyFile As String
    ...
    MyFile = File1.Path & “\” & File1.Filename
    If File1.Filename <> “” Then Open MyFile For Output As #1
    ...
You can set the Filename property in code, which results in the specified file becoming selected (highlighted) in the control on screen. A number of events are associated with the FileListBox control. Several of them are the standard events supported by most Visual Basic controls, such as Click. A couple of them, however, are special for this control. The PathChange event is triggered if the control’s Path or Filename properties are changed in code. The PatternChange property is triggered if the Pattern property is changed. These events are used to synchronize the operation of a FileListBox with other parts of the program. #### DirListBox The Directory List Box—or DirListBox—displays a list of folders. The user can navigate the folder structure of a drive with this list. Selecting a folder in a DirListBox control actually changes the current active path. By synchronizing a DirListBox with a FileListBox, you can easily provide a display of files in a folder selected by the user. The Path property sets and returns a DirListBox’s current path. The control will display the name of the current folder, with an open folder icon next to it. Above will be a hierarchical display of parent folders (if any), up to and including the root folder on the current drive. Below will be a list of any subfolders located in the current folder. This hierarchy is illustrated in Figure 14.3. In this figure, the current folder is SAMPLES. Its parent folder is MSVC20, which is located in the root folder on drive D:, indicated by the backslash. Within the SAMPLES folder are five subfolders: MFC, OLE2, and so on. To make any visible folder current, the user must double-click on it. The DirListBox’s Change event is executed when the control’s Path property changes, either by the user double-clicking on a folder in the list or by programming it in the code. You can use this event to synchronize a FileListBox with the DirListBox. The code shown here will result in the FileListBox’s display updating to show files in any new folder selected in the DirListBox:
    Private Sub Dir1_Change()
        File1.Path = Dir1.Path
    End Sub
Figure 14.3 A Directory List Box. #### DriveListBox The Drive List Box control (DriveListBox) displays a list of available drives at runtime. It takes the form of a drop-down list. When closed, the list displays the letter of the current drive. The user can pull down the list and select another drive; doing so not only changes the display in the control, but actually changes the system’s current drive. The control’s Drive property returns or sets the current drive. By assigning a DriveListBox’s Drive property to a DirListBox’s Path property, you can synchronize the two controls, so that the DirListBox will display folders on the drive selected in the DriveListBox. This step is done in the DriveListBox’s Change event procedure, which is triggered any time the Drive property changes (in code or by the user). Here’s the code:
    Private Sub Drive1_Change()
        Dir1.Path = Drive1.Drive
    End Sub
If the DirListBox is synchronized with a FileListBox, as described earlier, the three controls can be combined to provide a dialog box where the user can select a drive, select a folder, and then select a file. This combination is at the heart of implementing a File Open dialog. ### Demonstrating The File-Related Controls To demonstrate using the file-related controls, I first considered showing you how to create your own File Open or File Save dialog box. The Common Dialog control provides us with perfectly good Open and Save dialogs, however, and I wanted to come up with something that might actually be useful. A file-finding utility seemed like a good choice. I often have use for this program, so other people are also likely to need it. A file-finding utility is simple in concept. The user specifies a drive, a starting folder, and a file template. The program searches the specified folder and all of its subfolders for files that match the template, displaying them in a list. But how do you implement this in code? On the surface, it sounds rather complicated. If you approached the problem from a brute-force perspective, your solution might well end up being complicated. Some inside knowledge of how the DirListBox (Directory List Box) control works can greatly simplify the solution. This is an excellent example of how an intimate knowledge of Visual Basic and all its parts is essential to solving programming problems with maximum efficiency. #### Finding The Right Tool For The Job Let’s review the problem. You already know how to search a specific folder for files that match a template—just use the Dir function that was covered earlier in the chapter. The problem lies in extending the search to the subfolders of the starting folder, to their subfolders, and onward until the entire tree has been searched. If you closely examine the characteristics of the DirListBox control, you’ll find the solution. At runtime, a DirListBox displays the drive’s hierarchical folder structure. Above the current folder are listed the parent folder, its parent, and so on—back to the drive’s root directory. Below the current folder are listed its subfolders. Each item in this list has an index number, as follows: • The current folder’s index is -1. • Folders above the current folder have negative indexes. • The number of subfolders in the current folder is indicated by the control’s ListCount property. These subfolders have indexes starting at 0 and running to ListCount -1.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.